package com.jkc.xh.activities;

import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.jkc.xh.views.widget.ErrorView;
import com.jkc.xh.views.widget.ErrorView_;
import com.jkc.xh.views.widget.LoadingView;
import com.jkc.xh.views.widget.LoadingView_;
import com.jkc.xh.views.widget.ProgressDialog;
import com.jkc.xh.views.widget.ProgressDialog_;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

import retrofit.Call;

/**
 * Created by wangjie_91 on 2015/7/1.
 */

public abstract class BaseActivity extends AppCompatActivity {


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private boolean isLoadingShown;
    private ProgressDialog mProgressDlg;
    private LoadingView mLoadingPage;

    protected ArrayList<Call> mCallArray = new ArrayList<>();

    protected void showLoading() {
        showLoading(View.NO_ID);
    }

    protected void showLoading(int layoutId) {
        if (!isLoadingShown) {
            isLoadingShown = true;
            if (mLoadingPage == null) {
                mLoadingPage = LoadingView_.build(this);
            }
            displayLoadingPage(layoutId, mLoadingPage);
        }
    }

    protected void dismissLoadingPage() {
        if (mLoadingPage != null && isLoadingShown) {
            ViewGroup parent = (ViewGroup) mLoadingPage.getParent();
            if (parent != null) {
                isLoadingShown = false;
                parent.removeView(mLoadingPage);
            }
        }
    }


    private ErrorView mErrorPage;

    protected void showError() {
        showLoading(View.NO_ID);
    }

    protected void showError(int layoutId) {
        if (mErrorPage == null) {
            mErrorPage = ErrorView_.build(this);
            mErrorPage.setOnClickListener(createErrorPageClickListener());
        }
        if (!mErrorPage.isShown()) {
            if (mErrorPage.getParent() != null) {
                dismissErrorPage();
            }
            displayLoadingPage(layoutId, mErrorPage);
        }
    }

    private View.OnClickListener createErrorPageClickListener() {
        return new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dismissErrorPage();
                onErrorClick(v);
            }
        };
    }

    protected void onErrorClick(View view) {
    }

    protected void dismissErrorPage() {
        if (mErrorPage != null) {
            ViewGroup parent = (ViewGroup) mErrorPage.getParent();
            if (parent != null) {
                parent.removeView(mErrorPage);
            }
        }
    }

    boolean displayLoadingPage(int layoutId, View view) {
        ViewGroup layout = null;
        if (getView() != null) {
            int index = -1;
            if (layoutId != View.NO_ID) {
                layout = (ViewGroup) getView().findViewById(layoutId);
                //TODO
                if (layout != null && layout instanceof View && !(layout instanceof ViewGroup)) {
                    layout = (ViewGroup) layout.getParent();
                    if (layout instanceof LinearLayout) {
                        index = 0;
                    }
                }
            } else {
                layout = ((ViewGroup) this.getView());
            }
            if (layout != null) {
                layout.addView(view, index, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT));
            }
        }
        return layout != null;
    }

    public BaseActivity getActivity() {
        return this;
    }

    public View getView() {
        return getWindow().getDecorView();
    }

//    public void showProgressView() {
//        showProgressView(null, View.NO_ID);
//    }

    // public void showProgressViewInLocation(int layoutId) {
    // showProgressView(null, layoutId);
    // }

//    public void showProgressView(int resId) {
//        showProgressView(getString(resId), View.NO_ID);
//    }

    // public void showProgressViewInLocation(int resId,int layoutId) {
    // showProgressView(getString(resId),layoutId);
    // }

    private boolean displayView(View view, int layoutId) {
        ViewGroup layout = null;
        if (view != null) {
            int index = -1;
            if (layoutId != View.NO_ID) {
                layout = (ViewGroup) findViewById(layoutId);
            } else {
                layout = (ViewGroup) getWindow().getDecorView();
            }
            if (layout != null) {
                if (layout instanceof LinearLayout) {
                    index = 0;
                }
                layout.addView(view, index, new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            }
        }
        return layout != null;
    }

    ProgressDialog mProgressDialog;

    protected ProgressDialog showProgress() {
        return showProgress((ViewGroup) getView());
    }


    protected ProgressDialog showProgress(ViewGroup viewParent) {
        if (mProgressDialog == null) {
            mProgressDialog = ProgressDialog_.build(getActivity());
            mProgressDialog.addToView(viewParent);
        }
        mProgressDialog.show();
        return mProgressDialog;
    }

    protected void closeProgress() {
        if (mProgressDialog != null) {
            mProgressDialog.hide();
        }
    }


    public void showProgress(@StringRes int msgRes) {
        ProgressDialog dialog = showProgress();
        dialog.setTipMsg(msgRes);
    }

    public void showProgress(String msg) {
        ProgressDialog dialog = showProgress();
        dialog.setTipMsg(msg);
    }


//    public void showProgressView(String msg, int layoutId) {
//        if (mProgressDlg == null) {
//            mProgressDlg = new ProgressDialog(this);
//        }
//        if (mProgressDlg.isShowing()) {
//            return;
//        }
//        if (TextUtils.isEmpty(msg)) {
//            mProgressDlg.addToView();
//        } else {
//            mProgressDlg.addToView(msg);
//        }
//        displayView(mProgressDlg, layoutId);
//    }
//
//    public void closeProgressView() {
//        if (mProgressDlg != null && mProgressDlg.isShowing()) {
//            mProgressDlg.close();
//            ViewGroup parent = (ViewGroup) mProgressDlg.getParent();
//            if (parent != null) {
//                parent.removeView(mProgressDlg);
//            }
//        }
//    }

    public boolean progressViewIsShowing() {
        if (mProgressDlg != null) {
            return mProgressDlg.isShowing();
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        clearAllJob();
    }

    protected void addJob(Call call) {
        mCallArray.add(call);
    }

    protected void clearAllJob() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                if (mCallArray != null && !mCallArray.isEmpty()) {
                    for (Call call : mCallArray) {
                        if (call != null) {
                            call.cancel();
                        }
                    }
                }
            }
        }).start();

    }


//    protected void showSimLoading() {
//        DialogUtil.showSimLoadingDialog(getActivity(),true);
//    }
//
//    protected  void dismissSimLoading(){
//        DialogUtil.dismissSimLoadingDialog();
//    }


}
